package com.fnzn.entranceGuard.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.fnzn.entranceGuard.entity.GuardIcInfo;
import com.fnzn.entranceGuard.service.IGuardIcDeviceService;
import com.fnzn.entranceGuard.service.IGuardIcInfoService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R;
import com.github.pig.common.util.ResponseUtil;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static java.lang.System.out;

/**
 * <p>
 * IC信息管理 前端控制器
 * </p>
 *
 * @author JokerGao
 * @since 2018-06-25
 */
@RestController
@RequestMapping("/guardIcInfo")
public class GuardIcInfoController extends BaseController {
    @Autowired
    private IGuardIcInfoService guardIcInfoService;
    @Autowired
    private IGuardIcDeviceService guardIcDeviceService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return GuardIcInfo
     */
    @GetMapping("/{id}")
    public R<GuardIcInfo> get(@PathVariable String id) {
        return new R<>(guardIcInfoService.selectById(id));
    }


    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/page/{houseOwnerIdOrUserId}")
    @ResponseBody
    public Map page(@RequestParam Map<String, Object> params, @PathVariable String houseOwnerIdOrUserId) {
        Page<GuardIcInfo> page = new Page<GuardIcInfo>(Integer.parseInt(params.getOrDefault("page", 1).toString())
                , Integer.parseInt(params.getOrDefault("limit", 10).toString()));
        Map<String,Object> result = new HashMap<>();
        if("0".equals(params.get("type"))){
            //用户模块的请求
            result.put("data",guardIcInfoService.selectByUserId(page,houseOwnerIdOrUserId));
            result.put("code",guardIcInfoService.selectCodeByUserId(houseOwnerIdOrUserId));
        }else{
            result.put("data",guardIcInfoService.selectByHouseOwnerId(page,houseOwnerIdOrUserId));
            result.put("code",guardIcInfoService.selectCodeByHouseOwnerId(houseOwnerIdOrUserId));
        }
        return result;
    }

    /**
     * 通过业主id添加
     * @param  guardIcInfo  实体
     * @return success/false
     */
    @PostMapping
    @RequestMapping("/addByHouseOwnerId")
    public ResponseUtil addByHouseOwnerId(@RequestBody GuardIcInfo guardIcInfo) {
        guardIcInfo.setDelFlag(CommonConstant.STATUS_NORMAL);
        guardIcInfo.setCreateUser(getUserName());
        return guardIcInfoService.addByHouseOwnerId(guardIcInfo);
    }

    /**
     * 通过用户id添加
     * @param  guardIcInfo  实体
     * @return success/false
     */
    @PostMapping
    @RequestMapping("/addByUserId")
    public ResponseUtil addByUserId(@RequestBody GuardIcInfo guardIcInfo) {
        guardIcInfo.setDelFlag(CommonConstant.STATUS_NORMAL);
        guardIcInfo.setCreateUser(getUserName());
        return guardIcInfoService.addByUserId(guardIcInfo);
    }

    /**
     * 删除
     * @param guardIcInfo  实体
     * @return success/false
     */
    @DeleteMapping
    public ResponseUtil delete(@RequestBody GuardIcInfo guardIcInfo) {
        guardIcInfo.setUpdateUser(getUserName());
        guardIcInfo.setUpdateTime(new Date());
        return guardIcInfoService.deleteIcInfoById(guardIcInfo);
    }

    /**
     * 编辑
     * @param  guardIcInfo  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody GuardIcInfo guardIcInfo) {
        guardIcInfo.setUpdateUser(getUserName());
        guardIcInfo.setUpdateTime(new Date());
        return new R<>(guardIcInfoService.updateById(guardIcInfo));
    }

    /**
     * 根据id查询IC卡信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/getIcInfoById/{id}")
    @ResponseBody
    public Page getIcInfoById(@RequestParam Map<String, Object> params, @PathVariable String id) {
        Page<GuardIcInfo> page = new Page<GuardIcInfo>(Integer.parseInt(params.getOrDefault("page", 1).toString())
                , Integer.parseInt(params.getOrDefault("limit", 10).toString()));
        return guardIcInfoService.getIcInfoById(page,id);
    }

    /**
     * 同步
     * @param guardIcInfo
     * @return
     */
    @PutMapping
    @RequestMapping("/updateStatusByIcInfoId")
    public ResponseUtil updateStatusByIcInfoId(@RequestBody GuardIcInfo guardIcInfo) {
        return guardIcDeviceService.updateStatusByIcInfoId(guardIcInfo);
    }

    /**
     * 通过房屋同步（房屋所有业主下的Ic卡）
     * @param houseId
     * @return
     */
    @GetMapping("/updateStatusByHouseId/{houseId}")
    public ResponseUtil updateStatusByHouseId(@PathVariable("houseId") String houseId) {
        return guardIcDeviceService.updateStatusByHouseId(houseId);
    }

}
